package store.swing.order;

import store.domain.Order;
import store.swing.DefaultTable;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class OrderTable extends DefaultTable {
    public static final OrderTableModel TABLE_MODEL = new OrderTableModel();
    private static Order selectedOrder;
    public OrderTable() {
        super(TABLE_MODEL);

        addMouseListener(new TableMouseListener());
    }

    public static Order getSelectedOrder() {
        return selectedOrder;
    }

    private class TableMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                Integer orderId = (Integer) getValueAt(getSelectedRow(), 0);
                List<Order> orders = OrderTableModel.getOrders();
                for (Order order : orders) {
                    if (orderId.equals(order.getOrderId())) {
                        selectedOrder = order;
                        break;
                    }
                }
                new OrderInfoFrame(selectedOrder).setVisible(true);
            }
        }
    }
}
